xend: Fix VDI-VBD link for XenAPI
authorKeir Fraser <keir.fraser@citrix.com>
Fri, 9 Oct 2009 07:56:43 +0000 (08:56 +0100)
committerKeir Fraser <keir.fraser@citrix.com>
Fri, 9 Oct 2009 07:56:43 +0000 (08:56 +0100)
I detected problems of VDI-VBD link again.
  - In the case of inactive managed domains, VDI->VBD link was lost
    by xend restarting (or host OS rebooting).
  - In the case of active domains, both VDI->VBD link and VBD->VDI
    link were lost by xend restarting.

When xend is restarted, information of VDI instances is restored from
a vdi.xml file.  But the vdi.xml file does not have UUID of VBD
because xend does not write the UUID to the vdi.xml file.  Therefore,
VDI->VBD link is lost.  When xend is restarted, information of VBD
instances is restored from xenstore.  But xenstore does not have UUID
of VDI.  Therefore, VBD->VDI link is lost.

This patch solves the problems.  VDI instances stop having UUID of
VBD.  Instead, xend gathers UUID of VBD each time it's required.  The
method is the same as Network->VIF link.  Information of VBD instances
is restored not only from xenstore but from a config.sxp file.  UUID
of VDI is restored from the config.sxp file.

FYI, VBD->VDI link of inactive managed domains is not lost because
information of VBD instances is restored from the config.sxp file.
UUID of VDI is written by xend to the config.sxp file.

Signed-off-by: Masaki Kanno <kanno.masaki@jp.fujitsu.com>
tools/python/xen/xend/XendAPI.py
tools/python/xen/xend/XendDomain.py
tools/python/xen/xend/XendDomainInfo.py
tools/python/xen/xend/XendVDI.py

index f7bc1c8fb2ad019523f779870dbb5bc4b47916f6..6cb738e93e99daab410c1b1d73eb3aa7f18c04dc 100644 (file)
@@ -1986,8 +1986,6 @@ class XendAPI(object):
             log.exception("Error in VBD_create")
             return xen_api_error(['INTERNAL_ERROR', str(e)]) 
             
-        vdi.addVBD(vbd_ref)
-
         xendom.managed_config_save(dom)
         return xen_api_success(vbd_ref)
 
@@ -2004,8 +2002,6 @@ class XendAPI(object):
 
         XendTask.log_progress(0, 100, vm.destroy_vbd, vbd_ref)
 
-        vdi.removeVBD(vbd_ref)
-        
         return xen_api_success_void()
 
     def _VBD_get(self, vbd_ref, prop):
index 7a125fb23ae8c9eadca3994ce406596daed7d347..336e5bc8b2dc5440757c3478bef6744320def20c 100644 (file)
@@ -203,6 +203,17 @@ class XendDomain:
                             if key not in XendConfig.LEGACY_XENSTORE_VM_PARAMS and \
                                    key in dom:
                                 running_dom.info[key] = dom[key]
+                        # Devices information is restored from xenstore,
+                        # but VDI value in devices information can be not
+                        # restored because there is not VDI value in
+                        # xenstore. So we restore VDI value by using the
+                        # domain config file.
+                        for vbd_ref in running_dom.info['vbd_refs']:
+                            if dom['devices'].has_key(vbd_ref):
+                                r_devtype, r_devinfo = running_dom.info['devices'][vbd_ref]
+                                _, m_devinfo = dom['devices'][vbd_ref]
+                                r_devinfo['VDI'] = m_devinfo.get('VDI', '')
+                                running_dom.info['devices'][vbd_ref] = (r_devtype, r_devinfo)
                 except Exception:
                     log.exception("Failed to create reference to managed "
                                   "domain: %s" % dom_name)
@@ -347,7 +358,6 @@ class XendDomain:
             if self.is_domain_managed(dom):
                 self._managed_config_remove(dom.get_uuid())
                 del self.managed_domains[dom.get_uuid()]
-                dom.unlink_xapi_instances()
                 dom.destroy_xapi_instances()
         except ValueError:
             log.warn("Domain is not registered: %s" % dom.get_uuid())
@@ -486,7 +496,6 @@ class XendDomain:
             if domid in self.domains:
                 del self.domains[domid]
 
-            info.unlink_xapi_instances()
             info.destroy_xapi_instances()
         else:
             log.warning("Attempted to remove non-existent domain.")
index e9dfa15782bb2d47bcd36fed4aa319ca32387ffe..5c3b1f6c52984ddf0b73fa9cf7bfd577956db5f5 100644 (file)
@@ -4048,20 +4048,6 @@ class XendDomainInfo:
             except Exception, exn:
                 raise XendError('Failed to destroy device')
 
-    def unlink_xapi_instances(self):
-        from xen.xend import XendDomain
-        if XendDomain.instance().is_valid_vm(self.info.get('uuid')):
-            # domain still exists.
-            return
-
-        for vbd_ref in self.info.get('vbd_refs'):
-            dev_info = self.info['devices'].get(vbd_ref)[1]
-            vdi_uuid = dev_info.get('VDI', None)
-            if vdi_uuid and XendNode.instance().is_valid_vdi(vdi_uuid):
-                vdi = XendNode.instance().get_vdi_by_uuid(vdi_uuid)
-                if vdi.getVBDs().count(vbd_ref):
-                    vdi.removeVBD(vbd_ref)
-
     def destroy_xapi_instances(self):
         """Destroy Xen-API instances stored in XendAPIStore.
         """
index fa3d1479517128625c4c8d6b169ac6ef9d04de60..f8abea615f293dad830191797b36e58ab17798d2 100644 (file)
@@ -76,16 +76,17 @@ class XendVDI(AutoSaveObject):
         self.read_only = False
         self.type = "system"
         self.other_config = {}
-        self.vbds = []
-
-    def addVBD(self, vbd_ref):
-        self.vbds.append(vbd_ref)
-
-    def removeVBD(self, vbd_ref):
-        self.vbds.remove(vbd_ref)
 
     def getVBDs(self):
-        return self.vbds
+        from xen.xend import XendDomain
+        vbd_refs = [d.get_vbds() for d in XendDomain.instance().list('all')]
+        vbd_refs = reduce(lambda x, y: x + y, vbd_refs)
+        vbds = []
+        for vbd_ref in vbd_refs:
+            vdi = XendDomain.instance().get_dev_property_by_uuid('vbd', vbd_ref, 'VDI')
+            if vdi == self.uuid:
+                vbds.append(vbd_ref)
+        return vbds
 
     def load_config_dict(self, cfg):
         """Loads configuration into the object from a dict.
@@ -161,7 +162,7 @@ class XendVDI(AutoSaveObject):
                 'type': self.type,
                 'SR': self.sr_uuid,
                 'other_config': self.other_config,
-                'VBDs': self.vbds}
+                'VBDs': self.getVBDs()}
 
     def get_location(self):
         raise NotImplementedError()